﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.UI.WebControls;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.UI.WebControls;
using EPiServer.Web.WebControls;
using TemplateFoundation.PageTypeIcons.IconHandlers;
using TemplateFoundation.PageTypes;

namespace TemplateFoundation.PageTypeIcons
{
    
    /// <summary>
    /// <para>Helper class for Page Icons.</para>
    /// </summary>
    internal static class Extensions {

        /// <summary>
        /// Determines whether the specified <see cref="PageTreeNode"/> has a default node type icon.
        /// </summary>
        /// <param name="node">The node to check.</param>
        /// <returns>
        /// 	<c>true</c> if the specified node has a icon; otherwise, <c>false</c>.
        /// </returns>
        public static bool HasNodeIcon(this PageTreeNode node) {
            return node != null
                && node.TemplateContainer != null
                && node.TemplateContainer.Controls.Count > 0
                && node.TemplateContainer.Controls[0] is ThemeImage;
        }

        /// <summary>
        /// <para>Gets the status icon handlers for the given page type.</para>
        /// </summary>
        /// <param name="pageType">The page type to get handlers for.</param>
        public static IList<IconHandlerBase> GetStatusIconHandlers(this PageType pageType) {
            var page = DataFactory.Instance.GetDefaultPageData(PageReference.WasteBasket, pageType.ID) as PageTypeBase;
            return page.GetStatusIconHandlers();
        }

        /// <summary>
        /// <para>Gets the status icon handlers for the given page.</para>
        /// </summary>
        /// <param name="page">The page to get handlers for.</param>
        public static IList<IconHandlerBase> GetStatusIconHandlers(this PageTypeBase page) {
            var handlers = new List<IconHandlerBase>();
            handlers.AddRange(
                TypeDescriptor
                    .GetProperties(page)
                    .Cast<PropertyDescriptor>()
                    .SelectMany(prop =>
                        prop.Attributes.OfType<StatusIconAttribute>(),
                        (prop, attribute) => new {
                            prop,
                            attribute
                        })
                    .Select(@t1 => @t1.attribute.Handler)
                    .Where(@t1 => @t1.IsMatch(page)));
            return handlers;
        }
       
        /// <summary>
        /// <para>Gets the default node icon handler for the specified page.</para>
        /// </summary>
        /// <param name="page">The page to get the node icon for.</param>
        public static Image GetPageTypeIcon(this PageTypeBase page) {

            object[] attributes = page.GetType().GetCustomAttributes(typeof(PageTypeIconAttribute), true);
            PageTypeIconAttribute nodeIcon = attributes.FirstOrDefault() as PageTypeIconAttribute;
            if (nodeIcon != null) {
                Image icon = new Image();
                icon.ImageUrl = nodeIcon.ImageUrl;
                icon.AlternateText = String.Empty;
                return icon;
            }
            return null;
        }

        /// <summary>
        /// <para>Gets the default node icon handler for the specified page type.</para>
        /// </summary>
        /// <param name="pageType">The page type to get the node icon for.</param>
        public static Image GetPageTypeIcon(this PageType pageType) {
            var page = DataFactory.Instance.GetDefaultPageData(PageReference.WasteBasket, pageType.ID) as PageTypeBase;
            return page.GetPageTypeIcon();
        }

        /// <summary>
        /// <para>Gets the node icon URL for the given page.</para>
        /// </summary>
        /// <param name="page">The page to get the node icon URL for.</param>
        public static string GetPageTypeIconUrl(this PageTypeBase page) {
            var image = page.GetPageTypeIcon();
            if (image != null)
                return image.ImageUrl;
            return String.Empty;
        }

        /// <summary>
        /// <para>Gets the node icon URL for the given page type.</para>
        /// </summary>
        /// <param name="pageType">The page type to get the node icon URL for.</param>
        public static string GetPageTypeIconUrl(this PageType pageType) {
            var page = DataFactory.Instance.GetDefaultPageData(PageReference.WasteBasket, pageType.ID) as PageTypeBase;
            return page.GetPageTypeIconUrl();
        }
    }
}